package yja.gzw.app.fragmentview

import android.content.Context
import android.os.CountDownTimer
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import yja.gzw.app.R
import yja.gzw.app.base.BaseFragment
import yja.gzw.app.databinding.FragmentYanzhengBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import yja.gzw.app.window.Chicun

class YanzhengFragment:BaseFragment<Mainviewmodel,FragmentYanzhengBinding> (R.layout.fragment_yanzheng){
    val chicun= Chicun()
    private var onDataListener: OnInputDataListener2? = null
    private var countDownTimer: CountDownTimer? = null
    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is OnInputDataListener2) {
            onDataListener = context
        }
    }
    override fun onDetach() {
        super.onDetach()
        onDataListener = null
    }
    override fun initView() {
        chicun.chicunwai(binding.yanjing2,0,0,15,0)
        chicun.chicun(binding.yanjing2,85,25)
        chicun.chicun(binding.emilName2, UserData.kuan,50)
        chicun.chicunwai(binding.emilName2,20,20,20,0)
        chicun.chicun(binding.buju2, UserData.kuan,50)
        chicun.chicunwai(binding.buju2,20,20,20,0)
        chicun.chicun(binding.emilPaw2, UserData.kuan,50)


        binding.emilName2.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                // 可以在这里处理文本变化前的逻辑
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                // 可以在这里处理文本变化时的逻辑
            }

            override fun afterTextChanged(s: Editable?) {
                val email = binding.emilName2.text.toString()
                val password = binding.emilPaw2.text.toString()
                onDataListener?.onInputData2(email, password)
            }
        })


        binding.yanjing2.setOnClickListener {
            val shoujihao=binding.emilName2.text.toString()
            viewmodel.onzhuceyzmwang(mapOf("mobile" to shoujihao ,"event" to "mobilelogin"))
            Toast.makeText(context,"获取验证码中请稍后...", Toast.LENGTH_SHORT).show()

            binding.yanjing2.isEnabled = false
            countDownTimer = object : CountDownTimer(60000, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    binding.yanjing2.text = "${millisUntilFinished / 1000}s"
                }

                override fun onFinish() {
                    binding.yanjing2.isEnabled = true
                    binding.yanjing2.text = "重新获取"
                }
            }.start()


        }
        binding.emilPaw2.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                // 可以在这里处理文本变化前的逻辑
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                // 可以在这里处理文本变化时的逻辑
            }

            override fun afterTextChanged(s: Editable?) {
                val email = binding.emilName2.text.toString()
                val password = binding.emilPaw2.text.toString()
                onDataListener?.onInputData2(email, password)
            }
        })




    }

    override fun dataObservese() {


        viewmodel.onzhuceyzm.observe(this, Observer {
            Log.e("开始","111")
            if(it.code==1){
                Log.e("开始","222")
                Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
            }else{
                Log.e("开始","333")
                Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
            }

        })



    }
}
interface OnInputDataListener2 {
    fun onInputData2(email: String, password: String)
}
